<template>
	<om-addpage style="padding-top: 35px;" :busCode="busCode" :menuName="menuName" @formSubmit="formSubmit">
		<om-autoform ref="autoform" :initConfigData="mainConfigData" :initValue="initValue"></om-autoform>
	</om-addpage>
</template>

<script>
	import {
		errorModal,
		ignoreHtml,
		getUrl,
		confirmModal,
		ptFloat,
		minusF
	} from '@/static/js/common.js';
	import {
		buildFormData,
		formValidation,
		tranferToEditors,
		changeEditor,
		setSubmitSubValue,
		configSelectData,
		hasValue,
		formShowLoading,
		getFormFields,
		formModelLoad,
		getWorkCacheKey,
		getEditorTitle
	} from '@/static/js/formUtil.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildFormData({
				userCacheKey:"mkt-zs-productGive-user-cache",
				currencyIdCacheKey:'mkt-zs-productGive-currency-cache',
				customerShowCache:'mkt-zs-productGive-customerShow-cache'
			})
		},
		computed: {
			...mapState(useLoginUserStore, ['userName', 'userId'])
		},
	
		methods:{
			verifySubDel(i,editor){
				let _that = this
				if(editor.groupKey=='title3'){
					let num = 0
					for(let index in editor.workList){
						if(index != i){
							num = (Number(num)  + Number(editor.workList[index].totalPrice)).toFixed(2)
						}
					}
					_that.$refs.autoform.formdata.priceSum = num
				}
			},
			clearCustomerShow(){
				let _that = this
				_that.$refs.autoform.clear("title1","customerShowId")
				_that.$refs.autoform.clear("title1","customerCodeX")
				_that.$refs.autoform.clear("title1","customerId")
				_that.$refs.autoform.clear("title1","customerShortNameX")
			},
			getCustomerByOwner(){
				let _that = this
				_that.$refs.autoform.changeEditor("title1","customerShowId",{
					link:'../../public/pageSearch/pageSearch',
					"linkParam":{
						other:JSON.stringify({
							userId: _that.$refs.autoform.formdata.userId,
							oldValue: '',
							flag: false,
							busCode: 'MAR0508',
							zuserId: _that.userId,
							zposId: _that.$refs.autoform.formdata.zposId
						}),
						url:'customerShow/getCustomerByOwner',
						initload:true,
						searchfield:'q',
						namefield:'customerName',
						notefield:'customerCode',
						cachename:_that.customerShowCache
					}
				})
			},
			clearUserPost(){
				let _that = this
				_that.$refs.autoform.clear("title1","userPostId")
				_that.$refs.autoform.clear("title1","postId")
				_that.$refs.autoform.clear("title1","instId")
				_that.$refs.autoform.clear("title1","mobile")
			},
			verifyTitle3(){
				let _that = this
				if(!_that.$refs.autoform.formdata.salesAttributeCategoryId){
					let title = _that.$refs.autoform.getEditorTitle("title1")
					let title1 = _that.$refs.autoform.getEditorTitle("title1","salesAttributeCategoryId")
					return title+'的'+title1+'需优先完成编辑！'
				}else{
					return true
				}
			},
			getWorkParam(){
				let _that = this
				return {
					salesAttributeCategoryId:_that.$refs.autoform.formdata.salesAttributeCategoryId
				}
			},
			onSelectZosId(item){
				let _that = this
				_that.om.request(
					'temployee/getStaffCombogrid',
					{
						postId:item.id,
						firstUserId:_that.$refs.autoform.formdata.userPostId,
						rows:20,
						page:1,
						q:''
					}
				).then(res=>{
					if(res.rows && res.rows.length > 0){
						let item = res.rows[0]
						_that.$refs.autoform.formdata.postId = item.salaryPosId
						_that.$refs.autoform.formdata.postName = item.salaryPosName
						_that.$refs.autoform.formdata.instId = item.salaryPosInstId
						_that.$refs.autoform.formdata.instName = item.salaryPosInstName
						_that.$refs.autoform.formdata.mobile = item.mobile
					}
					_that.$refs.autoform.formdata.userId = _that.userId
					_that.getCustomerByOwner()
				})
			},
			formSubmit(){
				let _that = this
				_that.formdata.userId = _that.formdata.userPostId
				if(_that.edit){
					_that.om.formSave("productGive/doUpdate",_that.formdata,_that)
				}else{
					_that.om.formSave("productGive/doAdd",_that.formdata,_that)
				}
			},
			initLoad(options){
				let _that = this
				let fileId = options.fileId
				let ajaxArr = [
					urlUtil.zposUrl(options.code),
					urlUtil.sysdictComboboxUrl("38550"),
					{
						url:'currencyDict/getLocalCurrency'
					},
					{
						url:'dictSource/loadCombo',
						param:{
							sourceId:'289',
							categoryId:'6710'
						}
					},
					{
						url:'dictSource/loadCombo',
						param:{
							sourceId:'289',
							categoryId:'7995'
						}
					}
				]
				if(fileId){
					ajaxArr.push({
						url:"productGive/getValue",
						param:{
							fileId:fileId
						}
					})
				}
				formModelLoad(ajaxArr,function(res,mainData){
				changeEditor("topinfo","zposId","data",configSelectData(res[0],"id","posname"),mainData)
				changeEditor("title1","userPostId",{
					defaultValue:_that.userId,
					defaultName:_that.userName
				},null,mainData)
				changeEditor("title1",'currencyId',{
					defaultName:res[2].value.codeNameUnit,
					defaultValue:res[2].value.id
				},null,mainData)
				changeEditor("title1","giveTypeId","data",configSelectData(res[3],"id","text"),mainData)
				changeEditor("title1","salesAttributeCategoryId","data",configSelectData(res[4],"id","text"),mainData)
				changeEditor("title1","currencyId",{
					link:'../../public/pageSearch/pageSearch',
					"linkParam":{
						namefield:'currencyCode',
						notefield:'currencyName',
						initload:true,
						searchfield:'q',
						pagination:true,
						cachename:_that.currencyIdCacheKey,
						url:'currencyDict/getCurrency',
						method:'get'
					}
				},null,mainData)
				changeEditor("title1","userPostId",{
					link:"../../public/pageSearch/pageSearch",
					"linkParam": {
						other: JSON.stringify({
						}),
						namefield: urlUtil.officePageUrl().namefield,
						searchfield: 'q',
						url: urlUtil.officePageUrl().url,
						cachename: _that.userCacheKey,
						placeholder: urlUtil.officePageUrl().prompt
					}
				},null,mainData)
				changeEditor("title2","giveTypeId","data",configSelectData(res[1],"id","dicname"),mainData)
				if(_that.subEditorObj["title3"]){
					let configData = _that.subConfigData["title3"]
					let title = getEditorTitle("title3","projectId",configData)
					let title2 = getEditorTitle("title3","projectName",configData)
					let title3 = getEditorTitle("title3","projectNum",configData)
					let title4 = getEditorTitle("title3","totalPrice",configData)
					changeEditor('title3',"productStr",{
						getWorkParam:'getWorkParam',
						subtitle1Template :title+":<%= projectCode || '未填写' %>",
						titleTemplate:title2+":<%= projectName || '未填写' %>",
						subtitle2Template:title3+":<%= projectNum || '未填写' %>",
						subtitle3Template:title4+":<%= totalPrice || '' %>",
					},null,mainData)
				}
				if(_that.subEditorObj["title5"]){
					let configData = _that.subConfigData["title5"]
					let title = getEditorTitle("title5","userPostId",configData)
					changeEditor('title5',"reviewerStr",{
						titleTemplate :title+":<%= userName || '未填写' %>",
					},null,mainData)
				}
				if(fileId){
					let fieldArr = getFormFields(mainData)
					fieldArr.push("id")
					for(let index in res[5].value){
						if(fieldArr.includes(index)){
							_that.$refs.autoform.$set(_that.$refs.autoform.formdata,index,res[5].value[index])
							_that.$set(_that.formdata,index,res[5].value[index])
						}
					}
					
					let dataList = _that._.cloneDeep(res[5].value.productList)
					_that.subEditorObj["title3"].workList = dataList
					_that.$refs.autoform.$set(_that.$refs.autoform.formdata,"productStr",JSON.stringify(dataList))
					_that.$set(_that.formdata,"productStr",JSON.stringify(dataList))
					delete res[5].value.dataList
					let reviewerList = _that._.cloneDeep(res[5].value.reviewerList)
					_that.subEditorObj["title5"].workList = reviewerList
					_that.$refs.autoform.$set(_that.$refs.autoform.formdata,"reviewerStr",JSON.stringify(reviewerList))
					_that.$set(_that.formdata,"reviewerStr",JSON.stringify(reviewerList))
					delete res[5].value.reviewerList
					changeEditor("title1","customerShowId",{
						link:'../../public/pageSearch/pageSearch',
						"linkParam":{
							other:JSON.stringify({
								userId: _that.$refs.autoform.formdata.userPostId,
								oldValue: '',
								flag: false,
								busCode: 'MAR0508',
								zuserId: _that.userId,
								zposId: _that.$refs.autoform.formdata.zposId
							}),
							url:'customerShow/getCustomerByOwner',
							initload:true,
							searchfield:'q',
							namefield:'customerName',
							notefield:'customerCode',
							cachename:_that.customerShowCache
						}
					},null,mainData)
					_that.$nextTick(function(){
						_that.$refs.autoform.initSelectIndex("title1","giveTypeId")
						_that.$refs.autoform.initSelectIndex("title1","salesAttributeCategoryId")
					})
				}
				_that.$nextTick(function(){
					if(!_that.$refs.autoform.formdata.zposId){
						_that.$refs.autoform.initSelectVal("topinfo","zposId")
					}else{
						_that.$refs.autoform.initSelectIndex("topinfo","zposId")
					}
				})
				},options)
			}
		},
		onLoad(options) {
			let _that = this
			_that.initLoad(options)
		},
		onUnload(){
			//#ifdef APP-PLUS
			let _that = this;
			if (_that.$refs.autoform) {
				for (var fieldId in _that.subEditorObj) {
					uni.removeStorage({
						key: getWorkCacheKey(fieldId)
					});
				}
			}
			//#endif
		},
		onShow() {
			let _that = this
			if(_that.$refs.autoform){
				for(var fieldId in _that.subEditorObj){
					let editor = _that.subEditorObj[fieldId];
					uni.getStorage({
						key:getWorkCacheKey(fieldId),
						success:(res)=>{
						editor.workList = res.data
						if(editor.groupKey=='title3'){
							let num = 0
							for(let item of editor.workList){
								num = (Number(num)  + Number(item.totalPrice)).toFixed(2)
							}
							_that.$refs.autoform.formdata.priceSum = num
						}
						uni.removeStorage({
							key: getWorkCacheKey(fieldId)
						});	
						}
					})
				}
				uni.getStorage({
					key:_that.userCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.userPostId = d.userId
							formdata.userName = d.userName
							formdata.userId = d.userId
							formdata.postId = d.posId
							formdata.postName = d.posName
							formdata.mobile = d.autoMobile
							formdata.instId = d.instId
							formdata.instName = d.instName
							_that.getCustomerByOwner()
							uni.removeStorage({
								key:_that.userCacheKey
							})
						}
					}
				})
				uni.getStorage({
					key:_that.currencyIdCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.currencyId = d.id
							formdata.currencyName = d.codeNameUnit
							uni.removeStorage({
								key:_that.currencyIdCacheKey
							})
						}
					} 
				})
				uni.getStorage({
					key:_that.customerShowCache,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.customerShowId = d.id
							formdata.customerNameX = d.customerName
							formdata.customerCodeX = d.customerCode
							formdata.customerId = d.customerId
							formdata.customerShortNameX = d.customerShortName
							uni.removeStorage({
								key:_that.customerShowCache
							})
						}
					} 
				})
			}
		}
	}
</script>

<style>
</style>

